#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: startDump.sh
# Description: 
#   
# Edit History: 
#   2010-12-07    File created.
#========================================================================

#目標目錄
target_dir="/data"
#目標檔案
target_file="tcpdump-arm"
#分析的檔案名稱
result_analysis="tcpdout.txt"

#check if android device has install tcpdump-arm
#寫一段adb shell command 看是否tcpdump-arm已經建立,然後執行,在關閉用wireshark來看
files=`adb shell ls $target_dir`

istcpdumpExist=false
for name in ${files[@]}; do
	if [[ $name == $target_file* ]] ; then
		istcpdumpExist=true
	fi
done

if $istcpdumpExist ; then
	echo "'$target_dir/$target_file' is found"
	adb shell chmod 755 $target_dir/$target_file
else
	echo "'$target_dir/$target_file' is not found"
	echo "install '$target_dir/$target_file' now"
	adb push $target_file $target_dir/$target_file
	adb shell chmod 755 $target_dir/$target_file
fi

echo "start to execute $target_file now"
#寫一個可以接收signal轉成ctrl-c 給adb shell終止或是用ps , adb shell kill -9 來結束
adb shell $target_dir/$target_file -s 0 -w $target_dir/$result_analysis >/dev/null &
ADB_PID=$(echo $!)

echo -n "stop dump to generate output '$result_analysis' now(y/n): "
read stop
if [ $stop == "y" ] ; then
	kill -9 ${ADB_PID}
	#先用ps ; adb shell kill
	adb pull $target_dir/$result_analysis .
fi


